package com.lxk.mall.pms.service.impl;

import com.lxk.mall.pms.domain.PmsProductFavorite;
import com.lxk.mall.pms.domain.bo.favorite.PmsProductFavoriteBo;
import com.lxk.mall.pms.mapper.PmsProductFavoriteMapper;
import com.lxk.mall.pms.service.IPmsProductFavoriteService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.lxk.mall.pms.domain.vo.favorite.PmsProductFavoriteVo;

import java.util.List;
import java.util.Map;
import java.util.Collection;

import static com.lxk.mall.pms.enums.PmsErrorCodeConstants.FAVORITE_EXISTS;

/**
 * 商品收藏Service业务层处理
 *
 * @author Lion Li
 * @date 2024-06-06
 */
@RequiredArgsConstructor
@Service
public class PmsProductFavoriteServiceImpl implements IPmsProductFavoriteService {

    private final PmsProductFavoriteMapper baseMapper;

    /**
     * 查询商品收藏
     *
     * @param id 主键
     * @return 商品收藏
     */
    @Override
    public PmsProductFavoriteVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询商品收藏列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商品收藏分页列表
     */
    @Override
    public TableDataInfo<PmsProductFavoriteVo> queryPageList(PmsProductFavoriteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PmsProductFavorite> lqw = buildQueryWrapper(bo);
        Page<PmsProductFavoriteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的商品收藏列表
     *
     * @param bo 查询条件
     * @return 商品收藏列表
     */
    @Override
    public List<PmsProductFavoriteVo> queryList(PmsProductFavoriteBo bo) {
        LambdaQueryWrapper<PmsProductFavorite> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PmsProductFavorite> buildQueryWrapper(PmsProductFavoriteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PmsProductFavorite> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, PmsProductFavorite::getUserId, bo.getUserId());
        lqw.eq(bo.getSpuId() != null, PmsProductFavorite::getSpuId, bo.getSpuId());
        return lqw;
    }

    /**
     * 新增商品收藏
     *
     * @param bo 商品收藏
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PmsProductFavoriteBo bo) {
        PmsProductFavorite add = MapstructUtils.convert(bo, PmsProductFavorite.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品收藏
     *
     * @param bo 商品收藏
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PmsProductFavoriteBo bo) {
        PmsProductFavorite update = MapstructUtils.convert(bo, PmsProductFavorite.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PmsProductFavorite entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除商品收藏信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Long createFavorite(Long userId, Long spuId) {
        PmsProductFavorite favorite = baseMapper.selectByUserIdAndSpuId(userId, spuId);
        if (favorite != null) {
            throw new ServiceException(FAVORITE_EXISTS);
        }
        PmsProductFavorite add = new PmsProductFavorite();
        add.setUserId(userId);
        add.setSpuId(spuId);
        baseMapper.insert(add);
        return add.getId();
    }

    @Override
    public void deleteFavorite(Long userId, Long spuId) {
        PmsProductFavorite favorite = baseMapper.selectByUserIdAndSpuId(userId, spuId);
        if (favorite != null) {
            throw new ServiceException(FAVORITE_EXISTS);
        }
        baseMapper.deleteById(favorite.getId());
    }

    @Override
    public PmsProductFavorite getFavorite(Long userId, Long spuId) {
        return baseMapper.selectByUserIdAndSpuId(userId, spuId);
    }

    @Override
    public Long getFavoriteCount(Long userId) {
        return baseMapper.selectCountByUserId(userId);
    }
}
